home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / multitsk.arc / TEST2.C < prev    next >
Encoding:
Text File  |  1986-03-05  |  2.4 KB  |  112 lines

  1. int conlock;
  2. int *comanch;
  3. struct INPUT
  4.    { struct INPUT *next;
  5.      char text[2];
  6.    };
  7. struct INPUT *diskqueue;
  8. char *getmem();
  9.  
  10. /*  This program tests the multi-tasking functions.
  11. *   Three functions are attached:
  12. *       1.  keytask, which just echoes characters from the keyboard
  13. *           to the console until a tilde (~) is encountered.
  14. *       2.  diskin, which copies the file "taskfunc.asm" to an internal
  15. *           queue, one full line at a time.
  16. *       3.  diskout, which dumps the internal queue, at most 10 chars
  17. *           at a time, to the file "test2.out"
  18. *
  19. *   The main task just waits until all subtasks have completed.
  20. *
  21. */
  22. #include "stdio.h"
  23.  
  24. main()
  25. {
  26.   int j;
  27.   int i;
  28.  
  29.  
  30.   inittask("main");
  31.   if (attach(getmem(100),getmem(1024),1024)) keytask();
  32.   if (attach(getmem(100),getmem(1024),1024)) diskin();
  33.   if (attach(getmem(100),getmem(1024),1024)) diskout();
  34.   while (taskcnt() > 1) { yield(); }
  35. }
  36.  
  37. keytask()
  38. {
  39.   char c;
  40.  
  41.  
  42. printf("\nKeyboard task activated");
  43. for (;;)
  44.  
  45.    {
  46.      if (iskbhit())
  47.        {
  48.         c = getch();
  49.         if (c == '\r') putch('\n');
  50.         putch(c);
  51.         if (c == '~')
  52.           {
  53.              printf("\nKeyboard task terminating");
  54.              stop();
  55.           }
  56.        }
  57.      yield();
  58.    }
  59. }
  60.  
  61. diskin()
  62. {
  63.   FILE *fp;
  64.   char buffer[256], *sp;
  65.   struct INPUT *nq, *wp;
  66.  
  67.   printf("\nRead task now active");
  68.  
  69.   fp = fopen("taskfunc.asm","r");
  70.   if (!fp)  {printf("\nUnable to open taskfunc.asm - terminating"); exit(3);}
  71.   while ((sp = fgets(buffer,256,fp)))
  72.     {
  73.       nq = (struct INPUT *) getmem( 8+sizeof(char *) + strlen(sp));
  74.       nq->next = NULL;
  75.       strcpy(nq->text, sp);
  76.       wp = (struct INPUT *) &diskqueue;
  77.       while (wp->next) { wp = wp->next; }
  78.       wp->next = nq;
  79.       yield();
  80.     }
  81.   fclose(fp);
  82.   printf("\n\nRead subtask terminated.");
  83.   stop();
  84. }
  85. diskout()
  86. {
  87.   FILE *fp;
  88.   char *sp;
  89.   int j;
  90.   struct INPUT *nq, *wp;
  91.   char c;
  92.  
  93.   fp = fopen("test2.out","w");
  94.   printf("\nWritetask now active");
  95.   while ((nq=diskqueue) || (taskcnt() > 3))
  96.     {
  97.       diskqueue = nq->next;
  98.       sp = nq->text;
  99.       j=0;
  100.       while (*sp)
  101.         { fputc(*sp++, fp);
  102.           if (j++>10) {yield(); j=0;}
  103.         }
  104.       rlsmem(nq,8+sizeof(char *) + strlen(nq->text));
  105.  
  106.       yield();
  107.     }
  108.   printf("\n\nWrite task terminating");
  109.   fclose(fp);
  110.   stop();
  111. }
  112.